package com.wang.tool.hook;

import com.wang.tool.util.SimpleLogUtil;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Description 程序钩子注册
 * @Author shaopeng
 * @Date 2022/7/23
 * @Version 1.0
 */
public class AppHookRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes hookAttr = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableAppHook.class.getName()));
        Class<? extends AppHook>[] hookTypes = (Class<? extends AppHook>[]) hookAttr.getClassArray("hooks");
        for (Class<? extends AppHook> hookType : hookTypes) {
            BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(hookType);
            SimpleLogUtil.info("register hook:{0}", hookType.getName());
            registry.registerBeanDefinition(hookType.getName(), definitionBuilder.getBeanDefinition());
        }
    }
}
